package io.netty.example.helloworld.server;

import io.netty.channel.*;

import java.net.InetAddress;
import java.util.Date;

@ChannelHandler.Sharable
public class ServerHandler extends SimpleChannelInboundHandler<String> {
    /**
           * 建立连接时，发送一条庆祝消息
     * */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws  Exception{
     // 为新连接发送庆祝
        ctx.write("Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
        ctx.write("It is " + new Date() + " now.\r\n");
        ctx.flush();
        System.err.println("一个连接上了了");

    }

    //业务逻辑处理
    public void channelRead0(ChannelHandlerContext ctx,String request) throws Exception{
        //Generate and write a response
        String response;
        boolean close = false;
        if(request.isEmpty()){
            response = "please type someThing.\r\n";
        }else if("bye".equals(request.toLowerCase())){
            response = "Have a good day!1\r\n";
        }else{
            response = "Did you say '" + request + "'?\r\n";
        }
        ChannelFuture future = ctx.write(response);
        System.err.println(response);
        if(close){
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx){
        ctx.flush();
    }
    //异常处理
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
        cause.printStackTrace();
        ctx.close();
    }
}
